const path = require('path');
const cleanPlugin = require('clean-webpack-plugin');
const ExtractPlugin = require('extract-text-webpack-plugin');
const HtmlPlugin = require('html-webpack-plugin');
const webpack= require('webpack');

module.exports = {
    entry:{
        index:'./src/js/index.js'
    },
    output:{
        path:path.resolve(__dirname,'./dist'),
        filename:'./js/[name]-[hash].js'
    },
    module:{
        rules:[
            {
                test:/\.scss$/,
                use:['style-loader','css-loader','sass-loader']
                // use:ExtractPlugin.extract({
                //     fallback:'style-loader',
                //     use:['css-loader','sass-loader']
                // })
            },
            {
                test:/\.(png|jpg|svg|gif)$/,
                use:{
                    loader:'url-loader',
                    options:{
                        // 1000 = 1kb
                        limit:1234,
                        name:'imgs/[name].[ext]',
                        publicPath:'/'
                    }
                }
            },
            {
                test:/\.html$/,
                use:{
                    loader:'html-loader',
                    options:{
                        attrs:[':src']
                    }
                }
            },
            {
                test:/\.js$/,
                loader:'babel-loader'
            }
        ]
    },
    plugins:[
        new cleanPlugin(),
        // new ExtractPlugin({
        //     filename:'./css/[name].css'
        // }),
        new HtmlPlugin({
            template:'./src/index.html',
            inject:true,
            chunks:['index'],
            // title:'首页',
            filename:'index.html'
        }),
        new webpack.HotModuleReplacementPlugin()
    ],
    devServer:{
        port:9191,
        // open:true,
        // contentBase:path.resolve(__dirname,'./bundle')
        before(app){
            app.get('/api/banner',(req,res,next) => {
                res.end(JSON.stringify({code:1,data:'banner'}))
            })
        },
        hot:true
        // proxy:{
        //     '/api9090':'http://localhost:9090',
        //     '/api9292':'http://localhost:9292'
        // }
    },
    resolve:{
        alias:{
            'css':path.resolve(__dirname,'./src/css'),
            'indexScss$':path.resolve(__dirname,'./src/css/index.scss')
        },
        extensions: [".js", ".scss",".css"]
    },
    devtool:'cheap-module-eval-source-map'
}

//基础配置  webpack.base.conf.js

//开发配置  webpack.dev.conf.js

//线上配置  webpack.prod.conf.js

// 开发环境：基础配置 + 开发配置

// 线上环境：基础配置 + 线上配置